package com.winteam.View;

import com.winteam.Bean.Client;
import com.winteam.Bean.RoomInfo;
import com.winteam.Controler.Select;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.Font;
import java.awt.Color;
import java.awt.event.*;
import java.util.Vector;

public class TeamBilling extends JFrame {

	private JPanel HAGroomPane;
	private JTextField idCodeField;
	private JTextField HAGNameField;
	private JTextField numField;
	private JTextField cashPleField;
	private JTextField dayNumField;
	private JTextField siteField;
	private JTextField remarksField,iphone;
	JLabel roomid,number;
	Select select = new Select();

	Vector<String> Header = new Vector<>();

	Vector<Vector<String>> LeftBody = new Vector<>();

	Vector<Vector<String>> rightBody = new Vector();

	JComboBox HAGBox,idTypeBox;
	/*
    要移除的行数
     */
	int r_row = 0;
	DefaultTableModel leftModel = new DefaultTableModel();
	DefaultTableModel rightModel = new DefaultTableModel();
	private int l_row =0 ;

	Color bg = new Color(231,215,183);
	 String price="";

	public static void main(String[] args) {
		new TeamBilling();
	}
	/**
	 * Create the frame.
	 */
	public TeamBilling() {
		setDefaultCloseOperation(1);
		this.setSize(688,810);
		HAGroomPane = new JPanel();
		HAGroomPane.setBackground(bg);
		HAGroomPane.setLayout(null);
		this.setLocationRelativeTo(null);
		setContentPane(HAGroomPane);
		
		JLabel roomInfolabel = new JLabel("房间信息");
		roomInfolabel.setBackground(Color.RED);
		roomInfolabel.setFont(new Font("宋体", Font.BOLD, 18));
		roomInfolabel.setBounds(105, 10, 94, 31);
		HAGroomPane.add(roomInfolabel);
		roomid = new JLabel("  ");
		number = new JLabel("0");
		JLabel roomTypelabel = new JLabel("房间类型:");
		roomTypelabel.setFont(new Font("宋体", Font.BOLD, 18));
		roomTypelabel.setBounds(39, 37, 94, 31);
		HAGroomPane.add(roomTypelabel);
		Header.add("房间编号");
		Header.add("房间单价");
		setLeftModel("标准单人间");
		rightModel.setDataVector(null,Header);
		JComboBox roomTypeBox = new JComboBox();
		roomTypeBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				String type=roomTypeBox.getSelectedItem().toString();

				roomid.setText("");
				rightBody.clear();
				if(leftModel != null){
					LeftBody.clear();
					leftModel.setDataVector(LeftBody,Header);
				}
				setLeftModel(type);
			rightModel.setDataVector(rightBody,Header);
			}
		});
		roomTypeBox.setFont(new Font("宋体", Font.BOLD, 14));
		roomTypeBox.setModel(new DefaultComboBoxModel(select.getRoomlx().toArray()));
		roomTypeBox.setBounds(143, 38, 120, 31);
		HAGroomPane.add(roomTypeBox);



		JTable lefttable = new JTable(leftModel);

		JScrollPane roomtypePane = new JScrollPane(lefttable);
		/**
		 * 左边table
		 *
		 */
		lefttable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				l_row = lefttable.getSelectedRow();

			}
		});
		roomtypePane.setBounds(10, 74, 301, 284);
		HAGroomPane.add(roomtypePane);
		
		JLabel billinglabel = new JLabel("开单房间");
		billinglabel.setFont(new Font("宋体", Font.BOLD, 18));
		billinglabel.setBounds(442, 10, 94, 31);
		HAGroomPane.add(billinglabel);
		
		JLabel idTypeLabel = new JLabel("证件类型:");
		idTypeLabel.setFont(new Font("宋体", Font.BOLD, 16));
		idTypeLabel.setBounds(10, 420, 78, 23);
		HAGroomPane.add(idTypeLabel);
		
		 idTypeBox = new JComboBox();
		idTypeBox.setBackground(bg);
		idTypeBox.setFont(new Font("宋体", Font.BOLD, 14));
		idTypeBox.setModel(new DefaultComboBoxModel(new String[] {"身份证", "工作证件", "其他证件"}));
		idTypeBox.setBounds(93, 417, 124, 30);
		HAGroomPane.add(idTypeBox);
		
		JLabel idCodeLabel = new JLabel("手机号码:");
		idCodeLabel.setFont(new Font("宋体", Font.BOLD, 16));
		idCodeLabel.setBounds(10, 463, 78, 23);
		HAGroomPane.add(idCodeLabel);
		
		idCodeField = new JTextField();
		idCodeField.setBounds(93, 457, 124, 29);
		HAGroomPane.add(idCodeField);
		idCodeField.setColumns(10);
		
		JLabel HAGLabel = new JLabel("主客性别:");
		HAGLabel.setFont(new Font("宋体", Font.BOLD, 16));
		HAGLabel.setBounds(458, 420, 78, 23);
		HAGroomPane.add(HAGLabel);
		
		HAGBox = new JComboBox();
		HAGBox.setBackground(bg);
		HAGBox.setModel(new DefaultComboBoxModel(new String[] {"男", "女"}));
		HAGBox.setFont(new Font("宋体", Font.BOLD, 14));
		HAGBox.setBounds(538, 417, 124, 30);
		HAGroomPane.add(HAGBox);
		
		JLabel HAGNameLabel = new JLabel("主客姓名:");
		HAGNameLabel.setFont(new Font("宋体", Font.BOLD, 16));
		HAGNameLabel.setBounds(233, 463, 78, 23);
		HAGroomPane.add(HAGNameLabel);
		
		HAGNameField = new JTextField();
		HAGNameField.setColumns(10);
		HAGNameField.setBounds(321, 457, 124, 29);
		HAGroomPane.add(HAGNameField);
		
		JLabel numLabel = new JLabel("宾客人数:");
		numLabel.setFont(new Font("宋体", Font.BOLD, 16));
		numLabel.setBounds(458, 463, 78, 23);
		HAGroomPane.add(numLabel);
		
		numField = new JTextField();
		numField.setColumns(10);
		numField.setBounds(538, 457, 124, 29);
		HAGroomPane.add(numField);
		
		JLabel cashPleLabel = new JLabel("实收押金:");
		cashPleLabel.setFont(new Font("宋体", Font.BOLD, 16));
		cashPleLabel.setBounds(10, 502, 78, 23);
		HAGroomPane.add(cashPleLabel);
		
		cashPleField = new JTextField();
		cashPleField.setColumns(10);
		cashPleField.setBounds(93, 496, 124, 29);
		HAGroomPane.add(cashPleField);
		
		JLabel lblNewLabel_2_2_2 = new JLabel("主客房间:");
		lblNewLabel_2_2_2.setFont(new Font("宋体", Font.BOLD, 16));
		lblNewLabel_2_2_2.setBounds(233, 502, 78, 23);
		HAGroomPane.add(lblNewLabel_2_2_2);
		
		JLabel dayNumLabel = new JLabel("预住天数:");
		dayNumLabel.setFont(new Font("宋体", Font.BOLD, 16));
		dayNumLabel.setBounds(458, 502, 78, 23);
		HAGroomPane.add(dayNumLabel);
		
		dayNumField = new JTextField();
		dayNumField.setColumns(10);
		dayNumField.setBounds(538, 496, 124, 29);
		HAGroomPane.add(dayNumField);
		
		JLabel siteLabel = new JLabel("地址信息:");
		siteLabel.setFont(new Font("宋体", Font.BOLD, 16));
		siteLabel.setBounds(10, 546, 78, 23);
		HAGroomPane.add(siteLabel);
		
		siteField = new JTextField();
		siteField.setColumns(10);
		siteField.setBounds(93, 544, 569, 29);
		HAGroomPane.add(siteField);
		
		JLabel remarksLabel = new JLabel("备注信息:");
		remarksLabel.setFont(new Font("宋体", Font.BOLD, 16));
		remarksLabel.setBounds(10, 593, 78, 23);
		HAGroomPane.add(remarksLabel);
		
		remarksField = new JTextField();
		remarksField.setColumns(10);
		remarksField.setBounds(93, 591, 569, 29);
		HAGroomPane.add(remarksField);

		JLabel iphoness = new JLabel("证件编号:");
		iphoness.setFont(new Font("宋体", Font.BOLD, 16));
		iphoness.setBounds(10, 640, 78, 23);
		HAGroomPane.add(iphoness);

		iphone = new JTextField();
		iphone.setColumns(10);
		iphone.setBounds(93, 640, 569, 29);
		HAGroomPane.add(iphone);
		
		JButton sureNewButton = new JButton("确定",new ImageIcon(getClass().getResource("img/modi3.gif")));
		sureNewButton.setBackground(bg);
		sureNewButton.setFont(new Font("宋体", Font.BOLD, 18));
		sureNewButton.setBounds(77, 694, 124, 41);
		HAGroomPane.add(sureNewButton);
		/**
		 * 确定按钮
		 */
		sureNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				Client c =Check();
				if( c !=null){
					if (select.addClients(c)) {
						String c_name = c.getC_name();
						int clientId = select.findClientId(c_name);
						//添加数据的方法
						addCheck_in(clientId);
						dispose();
					}
				}
			}
		});
		JButton cancelButton = new JButton("取消",new ImageIcon(getClass().getResource("img/cancel.gif")));
		cancelButton.setBackground(bg);
		cancelButton.setFocusPainted(false);
		sureNewButton.setFocusPainted(false);
		cancelButton.setFont(new Font("宋体", Font.BOLD, 18));
		cancelButton.setBounds(442, 694, 124, 41);
		HAGroomPane.add(cancelButton);
		/**
		 * 取消按钮
		 */
		cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		JTable righttable = new JTable(rightModel);
		righttable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				r_row=righttable.getSelectedRow();
			}
		});
		JScrollPane billingPane = new JScrollPane(righttable);
		/**
		 * 右边
		 */
		righttable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				r_row =righttable.getSelectedRow();
				System.out.println(l_row);

			}
		});
		billingPane.setBounds(340, 48, 301, 310);
		HAGroomPane.add(billingPane);
		
		JButton leftButton = new JButton("添加到开单区",new ImageIcon(getClass().getResource("img/new.gif")));
		leftButton.setBackground(bg);
		leftButton.setFont(new Font("宋体", Font.BOLD, 14));
		leftButton.setBounds(10, 358, 301, 31);
		HAGroomPane.add(leftButton);
		leftButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
			try {
				System.out.println(l_row);
				addRight(leftModel.getValueAt(l_row,0).toString());
				leftModel.removeRow(l_row);
				number.setText((rightBody.size())+"");
				if (rightBody.firstElement()!= null){
					roomid.setText(rightBody.firstElement().get(0).toString());
				}
			}catch (Exception e2){
				JOptionPane.showMessageDialog(null,"没有多余房间了");
				e2.printStackTrace();
				System.out.println("索引异常");
			}
				}
		});
		JButton rightButton = new JButton("从开单区删除",new ImageIcon(getClass().getResource("img/del.gif")));
		rightButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {

					addLight(rightModel.getValueAt(r_row,0).toString());
					rightModel.removeRow(r_row);
					number.setText((rightBody.size())+"");
				}catch (Exception e2){
					System.out.println("索引异常");
				}
			}
		});
		rightButton.setFont(new Font("宋体", Font.BOLD, 14));
		rightButton.setBackground(bg);
		rightButton.setBounds(340, 358, 301, 31);
		HAGroomPane.add(rightButton);
		


		roomid.setFont(new Font("宋体", Font.BOLD, 20));
		roomid.setForeground(Color.BLUE);
		roomid.setBounds(340, 501, 108, 23);
		HAGroomPane.add(roomid);
		
		JLabel label_1 = new JLabel("开单总数:");
		label_1.setBounds(233, 421, 110, 23);
		label_1.setFont(new Font("宋体", Font.BOLD, 20));
		
		
		HAGroomPane.add(label_1);
		

		number.setForeground(Color.RED);
		number.setFont(new Font("宋体", Font.BOLD, 25));
		number.setBounds(371, 415, 53, 31);
		HAGroomPane.add(number);
		this.setTitle("团体开单");
		this.setVisible(true);
	}

	private void addLight(String toString) {

		Vector v = new Vector();
		v.add(toString);
		v.add(price);
		LeftBody.add(v);

		leftModel.setDataVector(LeftBody,Header);

	}


	private void setLeftModel(String type) {
		for (RoomInfo r:select.selecRoomTypeinfos(type)){
			Vector<String> v = new Vector();
			 v.add(r.getR_rommber());
			v.add(r.getR_price()+"");
			price= String.valueOf(r.getR_price());
			 LeftBody.add(v);
		}
		leftModel.setDataVector(LeftBody,Header);
		number.setText(rightBody.size()+"");
	}
	private void addRight(String toString) {
		Vector v = new Vector();
		v.add(toString);
		v.add(price);
		rightBody.add(v);
		rightModel.setDataVector(rightBody,Header);
	}

	/**
	 * 获取用户输入的信息
	 */
	private Client Check(){

		String names = HAGNameField.getText();
		String iphones = idCodeField.getText();
		String sexs = this.HAGBox.getSelectedItem().toString();
		String sfzs = iphone.getText();
		String address = this.siteField.getText();
		String rember = this.remarksField.getText();

		if (names.equals("")&&sfzs.equals("")&&iphones.equals("")&&address.equals("")&&rember.equals("")){
			JOptionPane.showMessageDialog(null,"请填入完整信息");
			return null;
		}
		return new Client(0,names,sexs,"身份证",sfzs,address,iphones,rember);
	}

	private void addCheck_in(int clientId) {
		if (cashPleField.getText().equals("")&&dayNumField.getText().equals("")){
			JOptionPane.showMessageDialog(null,"请填入完整信息");
		}

		int roomId = 0;
		for (Vector<String> roomInfos : rightBody) {

			System.out.println(roomInfos.get(0));
			roomId = select.findRoomId(roomInfos.get(0));
			select.addCheck_in(roomId, clientId, rightBody.size(), cashPleField.getText(), dayNumField.getText());
			/**
			 * 把所有的的房间号都变成占用状态 roomId
			 */
			select.updateRoomState(roomId);
		}


		JOptionPane.showMessageDialog(null, "开单完成");

	}

}
